// pages/collectorSingle/collectorSingle.js
const app = getApp();
const api = require('../../apis/request.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    listdata: [
    ],
    stationName:"",
    updateTime:""
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var _this=this;
    // var data = unescape(JSON.stringify(options.data));
    var data = JSON.parse(unescape(options.data));
    // console.log("collectorSingle 获取到的参数 :" + data);
    api.requestCSRealtime(data.serial, function (result) {
      var res = result.data;
      // console.log("气象站实时数据:" + JSON.stringify(res));
      if (res.status != 0) {
        wx.showToast({
          title: '' + res.msg,
          icon: 'failed',
          duration: 2000,
          mask: true,
        })
        return;
      }
      //请求成功
      var labels = res.labels;
      var datas = res.datas;
      var itemList = new Array();
      for (var index in labels) {
        itemList.push(_this.createItem(datas, labels[index]))
      }
      // console.log("stationName = " + data.name + "\nupdateTime = " + datas[0].update_time)
      _this.setData({ stationName: data.name, updateTime: datas[0].update_time, listdata: itemList });
    });

    // wx.request({

    //   url: app.globalData.URL + "/data/realtimeSingle/" + data.serial,
    //   header: {
    //     "Accept": "application/json",
    //     "Content-Type": "application/json",
    //     "token": app.globalData.token
    //   },
    //   method: "GET",
    //   // data: { username: this.data.username, password: md5.hexMD5(this.data.password) },
    //   complete: function (result) {
    //     var res=result.data;
    //     // console.log("气象站实时数据:" + JSON.stringify(res));
    //     if(res.status!=0){
    //       wx.showToast({
    //         title: ''+res.msg,
    //         icon: 'failed',
    //         duration: 2000,
    //         mask: true,          
    //       })
    //       return;
    //     }
    //     //请求成功
    //     var labels = res.labels;
    //     var datas = res.datas;
    //     var itemList = new Array();
    //     for (var index in labels) {
    //       itemList.push(_this.createItem(datas,labels[index]))
    //     }
    //     // console.log("stationName = " + data.name + "\nupdateTime = " + datas[0].update_time)
    //     _this.setData({ stationName: data.name, updateTime: datas[0].update_time,listdata: itemList });
    //   }
    // });

  },

  createItem:function(datas,label){
    var _this=this;
    var dataKey = label.elementSort;
    var dataValue = datas[0][dataKey];
    var dataUnit = label.elementUnit;
    if(dataValue==undefined){
      dataValue="0";

    }
    var item = { "url": "", "name": label.elementName, "value": dataValue +" "+ dataUnit};

    // console.log("dataKey = " + dataKey);

    // console.log("dataValue = "+datas[0][dataKey]);
    switch (label.elementId) {
      case 1:
      case 39:
        //空气温度
        //超温度
        //组件温度1
        //组件温度2
        item.url = "../../img/elements/icon0_wendu.png";
        break;
      case 2:
        //相对湿度
        item.url = "../../img/elements/icon0_shidu.png";
        break;
      case 4:
      case 51:
        //底层风速
        //超风速
        item.url = "../../img/elements/icon0_fengsu.png";
        break;
      case 5:
      case 52:
        //底层风向
        //超风向
        item.url = "../../img/elements/icon0_fengxiang.png";
        break;
      case 8:
        //土壤温度1
        item.url = "../../img/elements/icon0_wendu_turang.png";
        break;
      case 9:
        //土壤湿度1
        item.url = "../../img/elements/icon0_shidu_turang.png";
        break;
      case 6:
      case 41:
      case 42:
      case 43:
        //降雨量
        //二十四小时雨量
        //连续雨量
        item.url = "../../img/elements/icon0_yuliang.png";
        break;
      case 12:
        //光照度
        item.url = "../../img/elements/icon0_guanzhaoqiangdu.png";
        break;
      case 30:
        //二氧化碳
        item.url = "../../img/elements/icon0_eryanghantan.png";
        item.name = "CO₂浓度";
        break;
      case 28:
        //PM2.5
        item.url = "../../img/elements/icon0_pm2p5.png";
        item.name = "PM₂.₅";
        break;

      case 29:
        //PM₁₀
        item.url = "../../img/elements/icon0_pm10.png";
        item.name = "PM₁₀";
        break;
      case 3:
        //大气压力
        item.url = "../../img/elements/icon0_daqiyali.png";
        break;
      case 7:
        //蒸发量
        item.url = "../../img/elements/icon0_zhengfaliang.png";
        break;
      case 40:
        //露点温度
        item.url = "../../img/elements/icon0_ludianwendu.png";
        break;
      case 14:
      case 16:
      case 18:
      case 20:
      case 22:
        //总辐射，斜面辐射，散射辐射，光合有效辐射
        item.url = "../../img/elements/icon0_fusheqiangdu.png";
        break;
      case 15:
      case 17:
      case 19:
      case 21:
      case 23:
        //总辐射累积量，斜面辐射累积量，散射辐射累积量
        item.url = "../../img/elements/icon0_fusheleiji.png";
        break;
      case 26:
        //日照时数
        item.url = "../../img/elements/icon0_rizhaoshichang.png";
        break;
      case 10:
      case 46:
        //土壤电导率
        item.url = "../../img/elements/icon0_turangdaodianlv.png";
        break;
      case 11:
      case 45:
        //土壤PH
        item.url = "../../img/elements/icon0_turangph.png";
        break;
      case 37:
      case 44:
        //叶面温度
        item.url = "../../img/elements/icon0_yemianwendu.png";
        break;
      case 38:
        //叶面湿度
        item.url = "../../img/elements/icon0_yemianshidu.png";
        break;
      case 27:
        //噪声
        item.url = "../../img/elements/icon0_zaosheng.png";
        break;
      case 31:
        //SO₂
        item.url = "../../img/elements/icon0_so2.png";
        item.name = "SO₂";
        break;
      case 35:
        //NO₂
        item.url = "../../img/elements/icon0_no2.png";
        item.name = "NO₂";
        break;
      case 36:
        //CO
        item.url = "../../img/elements/icon0_co.png";
        item.name = "CO";
        break;

      case 34:
        //H₂S
        item.url = "../../img/elements/icon0_h2s.png";
        item.name = "H₂S";
        break;
      case 33:
        //O₃
        item.url = "../../img/elements/icon0_o3.png";
        item.name = "O₃";
        break;
      case 49:
        //NH₃
        item.url = "../../img/elements/icon0_nh3.png";
        item.name = "NH₃";
        break;
      case 47:
        //溶解氧
        item.url = "../../img/elements/icon0_rongjieyang.png";
        break;
      case 48:
        //浊度
        item.url = "../../img/elements/icon0_zhuodu.png";
        break;
    }

   return item;
   
  },
 
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})